package com.hs.chd.action.data;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.hs.chd.service.IDataService;
import com.hs.core.action.BaseAction;
import com.hs.core.cache.DictCacheUtil;
import com.hs.core.db.DBMgr;
@Controller   
public class SummarizeController  extends BaseAction {
    private static final Logger logger = Logger.getLogger(SummarizeController.class);
	private IDataService dataService;
	@Autowired  
	public void setDataService(IDataService dataService) {
		this.dataService = dataService;
	}
	/**
	  * 综述数据库首页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/summarize/index.do", method = RequestMethod.GET)   
   public ModelAndView summindex(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 DBMgr dbo = this.getDbo(request);
         DictCacheUtil cacheUtil = DictCacheUtil.getInst();
         String bufferstr = cacheUtil.getDisplayStr("10");
         modelMap.put("catagorystr", bufferstr);
          
		 return new ModelAndView("app/summarize/summindex",modelMap);
		 
	 } 
	 /**
	  * 综述数据库预览
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/summarize/summview.do", method = RequestMethod.GET)   
   public ModelAndView summview(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 String tcmid = request.getParameter("tcmid");
		 DBMgr dbo = this.getDbo(request);
         String sql = "select id,tcmid,(select tagname from SCHD_TAGS where id=tcmid) as tcmname,SUMMCONTENT as content,to_char(CATAGORYID) as catacode,(select dictname from hs_code where dicttype='10' and dictcode=to_char(CATAGORYID)) as cataname from SCHD_TCM_SUMM where tcmid =? order by CATAGORYID";
         List datalist = dbo.queryList(sql, new Object[]{tcmid});
         modelMap.put("summlist", datalist);
          
		 return new ModelAndView("app/summarize/summarizeview",modelMap);
		 
	 } 
	 /**
	  * 综述数据库疾病树
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/summarize/distree.do", method = RequestMethod.GET)   
   public ModelAndView distree(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
 long start = System.currentTimeMillis()/1000;
		 
		 StringBuilder builder = new StringBuilder();
	 
			builder.append("{ id:4, pId:0, name:\"病名\", nocheck:true,open:true},\n");
 
			DBMgr dbutil = this.getDbo(request); 
			//builder.append("<item text=\"").append("病名分类").append("\"  open=\"0\" id=\"").append("4").append("\">");
			//builder.append("\n"); 
			List alldataList = dbutil.queryList("select a.*,to_char(a.id) as idstr,to_char(pid) as pidstr from SCHD_TAGS a where   pid=4   order by a.ordernum", null);
			logger.debug("alldataList:" + alldataList);
			for(int i=0;i<alldataList.size();i++){
				
				Map tag  = (Map)alldataList.get(i);
				logger.debug("map:" + tag);
				String tagname = (String)tag.get("tagname");
				String tagid = (String)tag.get("idstr");
				String tagpid = (String)tag.get("pidstr");
				String tagtype =  tag.get("tagtype") + "";
		 
 					builder.append("{ id:");
 					builder.append(tagid);
 					builder.append(", pId:");
 					builder.append(tagpid);
 					builder.append(", name:\"");
 					builder.append(tagname);
 					builder.append("\"");
 					builder.append("}");
 					if(i != alldataList.size() - 1){
 						builder.append(",");
 					}
 					builder.append("\n");  
				
			}
			  
		 
			logger.debug("start:" + start);
			
			logger.info("builder：" + builder.toString());
			 
//	    	 
	    	modelMap.put("treedata", builder.toString());
          
		 return new ModelAndView("app/summarize/distree",modelMap);
		 
	 } 
	  
	 /**
	  * 综述数据库框架页
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/summarize/summarizeframe.do", method = RequestMethod.GET)   
   public ModelAndView tagsframe(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 
		 return new ModelAndView("app/summarize/summarizeframe",modelMap);
		 
	 }  
	  
	 private static String cutStr(String str){
		 if(str.lastIndexOf(',') != -1){
			 str = str.substring(0, str.length()-1);
		 }
		 return str;
	 }
	 /**
	  * 异步加载疾病分类树
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/summarize/maintree.do")   
    public void maintree(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 long start = System.currentTimeMillis()/1000;
		 
		 StringBuilder builder = new StringBuilder();
	 
			builder.append("{ id:4, pId:0, name:\"病名\", open:true},\n");
 
			DBMgr dbutil = this.getDbo(request); 
			//builder.append("<item text=\"").append("病名分类").append("\"  open=\"0\" id=\"").append("4").append("\">");
			//builder.append("\n"); 
			List alldataList = dbutil.queryList("select a.*,to_char(a.id) as idstr from SCHD_TAGS a where   pid=4   order by a.ordernum", null);
			logger.debug("alldataList:" + alldataList);
			for(int i=0;i<alldataList.size();i++){
				
				Map tag  = (Map)alldataList.get(i);
				logger.debug("map:" + tag);
				String tagname = (String)tag.get("tagname");
				String tagid = (String)tag.get("idstr");
				String tagpid = (String)tag.get("pidstr");
				String tagtype =  tag.get("tagtype") + "";
		 
 					builder.append("{ id:");
 					builder.append(tagid);
 					builder.append(", pId:");
 					builder.append(tagpid);
 					builder.append(", name:");
 					builder.append(tagname);
 					builder.append(",nocheck:true");
 					builder.append("}");
 					if(i != alldataList.size() - 1){
 						builder.append(",");
 					}
 					builder.append(">\n");  
				
			}
			  
		 
			logger.debug("start:" + start);
			
			logger.info("builder：" + builder.toString());
			 
	    	try {
	    		 
				PrintWriter w = response.getWriter(); 
				 w.println(builder.toString());
				 w.flush();
				 w.close();
				 logger.debug("end:" + System.currentTimeMillis()/1000);
			} catch (IOException e) {
				 throw e;
			} 
	    	
//	    	StringBuilder builder = new StringBuilder();
//			 Map datacache = new HashMap();
//				builder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
//				builder.append("<tree id=\"0\">\n");
//				DBMgr dbutil = this.getDbo(request); 
//				builder.append("<item text=\"").append("病名分类").append("\"  open=\"0\" id=\"").append("4").append("\">");
//				builder.append("\n"); 
//				List alldataList = dbutil.queryList("select a.*,to_char(a.id) as idstr from SCHD_TAGS a where   pid=4   order by a.ordernum", null);
//				logger.debug("alldataList:" + alldataList);
//				for(int i=0;i<alldataList.size();i++){
//					
//					Map tag  = (Map)alldataList.get(i);
//					logger.debug("map:" + tag);
//					String tagname = (String)tag.get("tagname");
//					String tagid = (String)tag.get("idstr");
//					String tagpid = (String)tag.get("pidstr");
//					String tagtype =  tag.get("tagtype") + "";
//					    builder.append("<item ");
////					    DictCacheUtil dictutil = DictCacheUtil.getInst();
////					    StringBuilder builderstr = new StringBuilder();
//	 					builder.append("   open=\"0\" child=\"1\" checked=\"1\" radio=\"1\" id=\"").append(tagid).append("\" ");
//						builder.append(">\n"); 
//						builder.append("<itemtext><![CDATA["); 
//					    builder.append(tagname); 
//						builder.append("]]></itemtext>").append("\n");
//						builder.append("<userdata name=\"tagtype\">").append(tagtype).append("</userdata>\n"); 
//						
//						builder.append("</item>\n"); 
//					
//				}
//				  
//				builder.append("</item>\n"); 
//				builder.append("</tree>"); 
//				logger.debug("start:" + start);
//				
//				logger.info("builder：" + builder.toString());
//				 
//		    	try {
//		    		 
//					PrintWriter w = response.getWriter(); 
//					 w.println(builder.toString());
//					 w.flush();
//					 w.close();
//					 logger.debug("end:" + System.currentTimeMillis()/1000);
//				} catch (IOException e) {
//					 throw e;
//				} 
			
		 
	 } 
	 /**
	  * 异步加载树节点
	  * @param request
	  * @param response
	  * @param modelMap
	  * @return
	  * @throws ServletException
	  * @throws IOException
	  */
	 @RequestMapping(value="/summarize/loadnode.do")   
    public void loadnode(HttpServletRequest request,
				HttpServletResponse response,ModelMap modelMap) throws ServletException, IOException {
		 long start = System.currentTimeMillis()/1000;
		 String pid = request.getParameter("id");
		 StringBuilder builder = new StringBuilder();
		 builder.append("<?xml version='1.0' encoding='UTF-8'?>\n");
		 builder.append("<tree id=\""+pid+"\">\n");
		 logger.debug("pid:"+pid);
		  DictCacheUtil dictutil = DictCacheUtil.getInst();
		 
		 Map datacache = new HashMap(); 
			DBMgr dbutil = this.getDbo(request);  
			List alldataList = dbutil.queryList("select a.*,to_char(a.id) as idstr,to_char((select count(*) from SCHD_TAGS where pid=a.id)) as ischild from SCHD_TAGS a where  isdelete =1 and pid=?   order by a.ordernum", new Object[]{pid});
			for(int i=0;i<alldataList.size();i++){
				Map tag  = (Map)alldataList.get(i);
				String tagname = (String)tag.get("tagname");
				String tagid = (String)tag.get("idstr"); 
				String ischild = (String)tag.get("ischild"); 
				String tagtype =  tag.get("tagtype") + ""; 
			    builder.append("<item "); 
				builder.append(" open=\"0\" nocheckbox=\"1\"");
				if("0".equals(ischild)){
					builder.append(" child=\"0\" ");
				}else{
					builder.append(" child=\"1\" ");
				}
				builder.append("id=\"").append(tagid).append("\" ");
				builder.append(">\n"); 
				builder.append("<itemtext><![CDATA["); 
				 //查询中英文对照
			    if(null != dictutil.getSubjectcodeBycnKey(tagname)){
			    	builder.append(tagname).append("(").append(dictutil.getSubjectcodeBycnKey(tagname)).append(")");
			    }else{
			    	builder.append(tagname); 
			    }
				builder.append("]]></itemtext>").append("\n");
				builder.append("<userdata name=\"tagtype\">").append(tagtype).append("</userdata>\n"); 
				builder.append("</item>\n"); 
				
			}
			builder.append("</tree>\n");  
			 
			logger.debug("start:" + start);
			
			logger.info("builder：" + builder.toString());
		 
	    	try {
	    		 
				PrintWriter w = response.getWriter(); 
				 w.println(builder.toString());
				 w.flush();
				 w.close();
				 logger.debug("end:" + System.currentTimeMillis()/1000);
			} catch (IOException e) {
				 throw e;
			} 
			
		 
	 } 
}
